﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Yugi_Gui.Sort_Search
{
    partial class Sort
    {
        List<System.Windows.Controls.UserControl> list = new List<System.Windows.Controls.UserControl>();
        private System.Windows.Controls.UIElementCollection uIElementCollection;

        public Sort(System.Windows.Controls.UIElementCollection uIElementCollection)
        {
            this.uIElementCollection = uIElementCollection;
            foreach (System.Windows.Controls.UserControl tmp in uIElementCollection)
            {
                list.Add(tmp);
            }
        }
        public List<System.Windows.Controls.UserControl> Sortlist(int[] a)
        {
            quicksort(list, 0, list.Count-1, a);
            return list;
        }
        private void quicksort(List<System.Windows.Controls.UserControl> list, int l, int r, int[] a)
        {
            int i, j;
            int k;
            k = (l + r) / 2;
            i = l; j = r;
            do
            {
                while (!canswapT(list, a, i, k)) 
                    i++;
                while (!canswapT(list, a, k, j)) 
                    j--;
                if (i <= j)
                {
                    Swap(list, i, j);
                    i++; j--;
                }
            } while (i < j);
            if (l < j)
                quicksort(list, l, j, a);
            if (i < r)
                quicksort(list, i, r, a);
        }
        private void Swap<T>(List<T> list, int i, int j)
        {
            T t = list[i];
            list[i] = list[j];
            list[j] = t;
        }
    }
}
